﻿using UnityEngine;

namespace CWM.Skinn
{
    public static partial class SkinnContextCommands
    {
        //See SkinnContext.cs for Built In Material Commands

        [CtxMenuItem("Renderer/Material/Clone",
            "Clones the shared materials of a renderer for testing (Clone Materials are saved with the current scene.)"
            , 0)]
        public static void RendererMaterialsClone()
        {
            foreach (var item in SkinnContext.GetContextItems())
            {
                var context = item.GetComponent<Renderer>();
                if (SkinnEx.IsNullOrNotInAScene(context)) continue;
                if (!SkinnEx.HasMinimumRequirements(context)) return;
                SkinnContext.UndoRecord(context, "Clone Materials");
                var materials = context.GetSharedMaterialsFromSubMeshes();
                for (int i = 0; i < materials.Length; i++)
                {
                    if (!materials[i]) continue;
                    materials[i] = new Material(materials[i]) { hideFlags = HideFlags.None };
                }
                context.sharedMaterials = materials;
            }
            SkinnContext.Finished();
        }
    }
}